export default class NumberUtil {
  // 过滤表情
  static dealEmojion = (text) => {
    const regStr = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/gi;
    return text.replace(regStr, '');
  };

  static singleInput = (text) => {
    const newText = this.dealEmojion(text).split(' ').join('');
    if (newText.length > 20) {
      return newText.substr(0, 20);
    }
    return newText;
  };

  static multiLineInput = (text) => {
    const newText = this.dealEmojion(text).trim();
    if (newText.length > 300) {
      return newText.substr(0, 300);
    }
    return newText;
  };
  // 只能输入数字
  static onlyInputNum = (num) => num.replace(/[^\d]/g, '');

  // 数字输入框 数字输入框整数位<=9位，小数点两位。
  static numInput = (text) => {
    let mText = text.replace(/[^\d.]/g, '');
    mText = mText.replace(/\.{2,}/g, '.');
    mText = mText.replace(/^\./g, '');
    mText = mText.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
    mText = mText.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
    let relStr = '0';
    if (mText.toString().indexOf('.') != -1) {
      const newArr = mText.split('.');
      if (newArr[1].length > 0) {
        relStr = `${newArr[0].substr(0, 9)}.${newArr[1]}`;
      } else {
        relStr = mText;
      }
    } else {
      relStr = mText.substr(0, 9);
    }
    return relStr.toString();
  };
  // var toFloat = function (value) {
  //     value = Math.round(parseFloat(value) * 100) / 100;
  //     if (value.toString().indexOf(".") < 0) {
  //         value = value.toString() + ".00";
  //     }
  //     return value;
  // }
  static toPriceFloat(value = 0) {
    let val = parseFloat(value);
    val = isNaN(val) ? 0 : val.toFixed(2);
    return val;
  }

  static toPriceFloatThree(value = 0) {
    let val = parseFloat(value);
    val = isNaN(val) ? 0 : val.toFixed(3);
    return val;
  }

  // 末尾为点的转化成小数
  static dealEndPointNum = (text) => parseFloat(text);

  // 字符长度输入验证
  static inputFormatValide = (text, maxLength) => {
    // 去掉空格
    const newText = text.split(' ').join('');
    // 获取字符长度
    let len = 0;
    for (let i = 0; i < newText.length; i++) {
      let a = newText.charAt(i);
      if (this.isHanzi(a)) {
        len += 2;
      } else {
        len += 1;
      }
    }

    if (len > maxLength) {
      if (
        this.isHanzi(newText.charAt(newText.length - 1)) &&
        len != maxLength + 1
      ) {
        return newText.substr(0, newText.length - len + maxLength + 1);
      } else {
        return newText.substr(0, newText.length - len + maxLength);
      }
    }
    return newText;
  };

  // 验证是否是汉字
  static isHanzi = (text) => {
    return text.match(/[^\x00-\xff]/gi) != null;
  };
}
